package edu.osu.intelligentagents.common;

import java.util.Random;
import java.util.Vector;

public class Arm implements Comparable<Arm> {
	private static int count;
	public int id;
	public String name;
	public Vector<SBRD> sbrds;
	private static Random random;
	static {
		random = new Random(System.currentTimeMillis());
	}

	public Arm(String name, Vector<SBRD> sbrds) {
		synchronized (Arm.class) {
			this.id = count;
			count++;
		}
		this.name = name;
		float sum = 0;
		for (SBRD sbrd : sbrds) {
			sum += sbrd.p;
		}
		if (sum > 1)
			throw new RuntimeException(
					"Sum of the probability Can not be more than 1");
		this.sbrds = sbrds;
	}

	public float getReward() {
		float reward = 0;

		float r = random.nextFloat();
		float theta = 0;
		for (SBRD sbrd : sbrds) {
			theta += sbrd.p;
			if (r < theta) {
				reward = sbrd.reward;
				break;
			}
		}
		return reward;
	}

	public float getExpectedReward() {
		float expectedReward = 0f;
		for (SBRD sbrd : sbrds) {
			expectedReward += sbrd.p * sbrd.reward;
		}
		return expectedReward;
	}

	@Override
	public int compareTo(Arm o) {
		return this.name.compareTo(o.name);
	}

	@Override
	public String toString() {
		return name;
	}
}
